home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / ka9q_src.arc / TIMER.C < prev    next >
C/C++ Source or Header  |  1988-07-28  |  2KB  |  85 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "timer.h"
  4.  
  5. /* Head of running timer chain */
  6. struct timer *timers;
  7.  
  8. tick()
  9. {
  10.     register struct timer *t,*tp;
  11.     register struct timer *expired = NULLTIMER;
  12.  
  13.     /* Run through the list of running timers, decrementing each one.
  14.      * If one has expired, take it off the running list and put it
  15.      * on a singly linked list of expired timers
  16.      */
  17.     for(t = timers;t != NULLTIMER; t = tp){
  18.         tp = t->next;
  19.         if(tp == t){
  20.                 printf("PANIC: Timer loop at %lx\n",(long)tp);
  21.             fflush(stdout);
  22.             for(;;)
  23.                 ;
  24.                 }
  25.         if(t->state == TIMER_RUN && --(t->count) == 0){
  26.             stop_timer(t);
  27.             t->state = TIMER_EXPIRE;
  28.             /* Put on head of expired timer list */
  29.             t->next = expired;
  30.             expired = t;
  31.         }
  32.     }
  33.     /* Now go through the list of expired timers, removing each
  34.      * one and kicking the notify function, if there is one
  35.      */
  36.     while((t = expired) != NULLTIMER){
  37.         expired = t->next;
  38.         if(t->func){
  39.             (*t->func)(t->arg);
  40.         }
  41.     }
  42. }
  43. /* Start a timer */
  44. start_timer(t)
  45. register struct timer *t;
  46. {
  47.     char i_state;
  48.  
  49.     if(t == NULLTIMER || t->start == 0)
  50.         return;
  51.     i_state = disable();
  52.     t->count = t->start;
  53.     if(t->state != TIMER_RUN){
  54.         t->state = TIMER_RUN;
  55.         /* Put on head of active timer list */
  56.         t->prev = NULLTIMER;
  57.         t->next = timers;
  58.         if(t->next != NULLTIMER)
  59.             t->next->prev = t;
  60.         timers = t;
  61.     }
  62.     restore(i_state);
  63. }
  64. /* Stop a timer */
  65. stop_timer(t)
  66. register struct timer *t;
  67. {
  68.     char i_state;
  69.  
  70.     if(t == NULLTIMER)
  71.         return;
  72.     i_state = disable();
  73.     if(t->state == TIMER_RUN){
  74.         /* Delete from active timer list */
  75.         if(timers == t)
  76.             timers = t->next;
  77.         if(t->next != NULLTIMER)
  78.             t->next->prev = t->prev;
  79.         if(t->prev != NULLTIMER)
  80.             t->prev->next = t->next;
  81.     }
  82.     t->state = TIMER_STOP;
  83.     restore(i_state);
  84. }
  85.